package com.mz361.admin.market.controller;


import com.mz361.common.utils.StringUtil;
import com.mz361.component.shiro.ShiroUtil;
import com.mz361.modules.system.domain.PayOrder;
import com.mz361.modules.system.domain.User;
import com.mz361.modules.system.service.CompanyService;
import com.mz361.modules.system.service.PayOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author niceale
 * @ date 2024/06/19
 */
@Controller
@ApiIgnore
@RequestMapping("/market/salesOrder")
public class SalesOrderController {

    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private CompanyService companyService;

    /**
     * 列表页面
     */
    @GetMapping("/index")
    public String index(Model model, PayOrder payOrder) {
        Long totalPerformance, countOrder, countCompany;
        User user = ShiroUtil.getSubject();
        if (StringUtil.notEmpty(user.getSourceKey())) {
            payOrder.setSourceKey(user.getSourceKey());
            totalPerformance = payOrderService.getTotalPerformancebySourcekey(user.getSourceKey());
            countOrder = payOrderService.getCountOrderbySourcekey(user.getSourceKey());
            countCompany = companyService.countBySourceKey(user.getSourceKey());

        } else if (StringUtil.notEmpty(user.getInviteCode())) {
            payOrder.setInviteCode(user.getInviteCode());
            totalPerformance = payOrderService.getTotalPerformancebyInviteCode(user.getInviteCode());
            countOrder = payOrderService.getCountOrderbyInviteCode(user.getInviteCode());
            countCompany = companyService.countByInviteCode(user.getInviteCode());
        } else {
            totalPerformance = payOrderService.getTotalPerformance();
            countOrder = payOrderService.getCountOrder();
            countCompany = companyService.countAll();
        }
        payOrder.setResultStatus("SUCCESS");
        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("companyName", match -> match.contains());

        // 获取数据列表
        Example<PayOrder> example = Example.of(payOrder, matcher);
        Page<PayOrder> list = payOrderService.getPageList(example);

        // 封装数据
        model.addAttribute("totalPerformance", totalPerformance == 0 ? 0 : totalPerformance / 100);
        model.addAttribute("countOrder", countOrder);
        model.addAttribute("countCompany", countCompany);
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/market/salesOrder/index";
    }


    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("market:salesOrder:detail")
    public String toDetail(@PathVariable("id") PayOrder payOrder, Model model) {
        model.addAttribute("salesOrder", payOrder);
        return "/market/salesOrder/detail";
    }


}